//
//  MessageVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MessageVC.h"
#import "MessageCell.h"
#import "AFNetworking.h"

@interface MessageVC () {
    int pIntPage;
    
    NSString *strType;
}
@property(nonatomic,strong) NSMutableArray *messageData;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation MessageVC

- (void)initInfo
{
    [super initInfo];
    pIntPage = 1;
    strType = @"2";
    _messageData = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"消息"];
    
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pIntPage = 1;
        [weakSelf requestMessage];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestMessage];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
    
}
-(NSMutableArray *)messageDataDic{
    if (!_messageData) {
        _messageData = [NSMutableArray array];
    }
    return _messageData;
}

- (IBAction)btnSelectTypeAction:(UIButton *)sender {
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:1300];
    UIButton *btn2 = (UIButton *)[self.view viewWithTag:1301];
    UIButton *btn3 = (UIButton *)[self.view viewWithTag:1302];
    [btn1 setSelected:NO];
    [btn2 setSelected:NO];
    [btn3 setSelected:NO];
    sender.selected = YES;
    
    strType = [NSString stringWithFormat:@"%d", (int)sender.tag - 1300];
    pIntPage = 1;
    [self.pTableView.mj_header beginRefreshing];
}

-(void)requestMessage
{
    __weak typeof(self) weakSelf = self;
    [TDNetwork requestMessageListGetUrl:SocialMessageIP
                                andPage:pIntPage
                            andPageSize:10
                              msgStatus:strType
                               complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary)
    {
        if (succeed) {
            if (pIntPage == 1) {
                [_messageData removeAllObjects];
            }
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            NSLog(@"%@", dict);
            if (dict && [dict isKindOfClass:[NSDictionary class]]) {
                NSArray *array = [dict objectForKey:@"msgList"];
                [weakSelf.messageData addObjectsFromArray:array];
                
                if (array.count == 10) {
                    pIntPage ++;
                    [self.pTableView.mj_footer setHidden:NO];
                } else {
                    [self.pTableView.mj_footer setHidden:YES];
                }
            }
            [weakSelf.pTableView reloadData];
            
        } else {
        }
        
        [self.pTableView.mj_footer endRefreshing];
        [self.pTableView.mj_header endRefreshing];
        
        if (_messageData.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
    }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messageData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfMessageCell"];
    NSDictionary *dic =  [self.messageData objectAtIndex:indexPath.row];
//    cell.contentLB.text = [NSString stringWithFormat:@"%@", [dic valueForKey:@"content"]];
    [cell.uidNameLB setText:[NSString stringWithFormat:@"%@", [dic valueForKey:@"content"]]];
    [cell.releaseTimeLB setText:[dic valueForKey:@"time"]];
    [cell.messageImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP,[dic valueForKey:@"headImage"]]]];
    [cell.messageImageView.layer setCornerRadius:cell.messageImageView.frame.size.width / 2];
    [cell.messageImageView.layer setMasksToBounds:YES];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
